<!DOCTYPE html>
<html>  
  <head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="x-nav">
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input type="text" name="username"  placeholder="请输入名称" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <!-- <xblock>
          <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
      </xblock> -->
      <table id="catelist" lay-filter="catelist">
      </table>
    </div>
    <script>
      layui.use(['table','layer'], function(){
        $ = layui.jquery;
        var table = layui.table,
        layer = layui.layer;
        
        //表格数据填充
        table.render({
          elem: '#catelist',
          // height: 315,
          url: '/admin/listRecordByPage', //数据接口
          page: true, //开启分页
          loading:true,//加载显示
          text:{ none: '暂无相关数据'},
          done:()=>{layer.msg('加载完成！',{time:600});},
          cols: [[ //表头
            {checkbox: true},
            {field: 'app_his_id', title: '序号',type:'numbers'},
            {field: 'stu_name', title: '用户名'},
            {field: 'call_time', title: '通话时长',templet:'<div>{{d.call_time}}秒</div>'},
            {field: 'net_flow', title: '流量',templet:'<div>{{d.net_flow}}MB</div>'},
            {field: 'usephone_time', title: '手机使用时长',templet:'<div>{{parseInt(d.usephone_time/1000)}}秒</div>'},
            {field: 'sleep_time', title: '睡眠时间',templet:'<div>{{(d.sleep_time/3600).toFixed(2)}}小时</div>'},
            {field: 'step_count', title: '行走步数'},
            {field: 'music_time', title: '音乐播放',templet:'<div>{{parseInt(d.music_time/60)}}分钟</div>'},
            {field: 'date', title: '记录时间'},
            {title: '操作',width:120,templet: '#titleTpl'},
          ]]
        });
        //监听工具条
        table.on('tool(catelist)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
          var data = obj.data; //获得当前行数据
          var tr = obj.tr; //获得当前行 tr 的DOM对象
        
          if(obj.event === 'del'){ //删除
            layer.confirm('真的删除行么', function(index){
              //向服务端发送删除指令
              $.get('/admin/delRecord',{app_his_id:data.app_his_id},(data,status)=>{
                if(status=='success'&&data.code==200){
                  obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                  layer.msg('操作成功',{time:600});
                  layer.close(index);
                }else{
                  layer.msg('操作失败',{time:600});
                }
              });
            });
          } else if(obj.event === 'edit'){ //编辑
            //数据放入本地session
            window.sessionStorage.setItem('cate-item',JSON.stringify(data));
            //打开弹窗
            x_admin_show('编辑','cate-edit.html',450,450);
            //同步更新缓存对应的值
            obj.update(data);
          }
        });
        //========================================
      });
    </script>

    <script type="text/html" id="titleTpl">
      <button lay-event="edit" class="layui-btn layui-btn-danger layui-btn-sm"><i class="iconfont">&#xe69e;</i></button>
      <button lay-event="del" class="layui-btn layui-btn-danger layui-btn-sm"><i class="iconfont">&#xe69d;</i></button>
    </script>
  </body>

</html>